/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dpy.appmvdpy.Controllers;

import com.dpy.appmvdpy.Facades.DefaultFacade;
import com.dpy.appmvdpy.Facades.UsersFacade;
import com.dpy.appmvdpy.Facades.UsersRoleFacade;
import com.dpy.appmvdpy.Models.UserRoles;
import com.dpy.appmvdpy.Models.Users;
import com.dpy.appmvdpy.Models.Validators.DefaultValidator;
import com.dpy.appmvdpy.Models.Validators.UsersValidator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Lilli
 */

@Controller
@RequestMapping("/users")
public class UsersController extends DefaultController<Users> {
    
    @Autowired
    public UsersFacade facade;    
    @Autowired
    public UsersValidator validator;    
    @Autowired
    public UsersRoleFacade roleFacade;
    
    public UsersController()
    {
        super(Users.class,"users");
    }

    @Override
    public DefaultFacade<Users> getFacade() {
        return this.facade;
    }

    @Override
    public Users createObject() {
        return new Users();
    }

    @Override
    @Secured("ROLE_ADMIN")
    public DefaultValidator<Users> getValidator() {
        return validator;
    }

    @RequestMapping(value="/save",method= RequestMethod.POST)
    @Override
    @Secured("ROLE_ADMIN")
    public ModelAndView create(@ModelAttribute("model")Users model, BindingResult result, SessionStatus status) {
        
        model.setEnabled(true);
        ModelAndView retorno = super.create(model, result, status);
        
        if("/users/add".equals(retorno)) return retorno;
        
        int userId = 0; 
        
        if(model.getUserId() != null) 
           userId = model.getUserId();
        
        List<UserRoles> list = roleFacade.getRolesForUser(userId);
        UserRoles roles = null;
        
        if(list.isEmpty())
           roles = new UserRoles();
        else
           roles = list.get(0);
        
           roles.setAuthority("ROLE_USER");
           roles.setUserId(model);
     
        if(list.isEmpty())  
           roleFacade.add(roles);
        else
           roleFacade.edit(roles);  
         
        return retorno;
    }

    @Override
    @Secured("ROLE_ADMIN")
    public ModelAndView search(String name) {
        return super.search(name);
    }

    @Override
    @Secured("ROLE_ADMIN")
    public ModelAndView edit(int id) {
        return super.edit(id);
    }

    @Override
    @Secured("ROLE_ADMIN")
    public ModelAndView list(@RequestParam(required=false,defaultValue="0") int pag) {
        return super.list(pag);
    }

    @Override
    @Secured("ROLE_ADMIN")
    public ModelAndView create() {
        return super.create();
    }   
 
    
}
